﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Avalonia.Controls;
using static System.Net.Mime.MediaTypeNames;
using static System.Runtime.CompilerServices.RuntimeHelpers;


namespace TTEngine.Impl.Desktop
{
    class Impl_Input : IInput
    {

        List<KeyCode> presskeys = new List<KeyCode>();
        List<InputPoint> points = new List<InputPoint>();
        public void ParseKey(IView view, KeyCode keycode, bool press)
        {
            if (presskeys.Contains(keycode) && !press)
            {
                presskeys.Remove(keycode);
            }
            else
            {
                presskeys.Add(keycode);
            }
            if (OnKey != null)
            {
                view.InvokeGfxThread((gl) =>
                {
                    OnKey.Invoke(keycode, press);
                });
            }
        }
        public void ParseInputText(IView view, string text)
        {
            if (OnInputText != null)
            {
                view.InvokeGfxThread((gl) =>
                {
                    OnInputText?.Invoke(text);
                });

            }

        }
        int lastmousex = 0;
        int lastmousey = 0;
        bool lastmouseleftbtn = false;
        bool lastmousemidbtn = false;
        bool lastmouserightbtn = false;
        public void ParseMouse(IView view, MouseButton button, int x, int y, bool press)
        {
            bool moved = false;
            bool presschange = false;
            if (button == MouseButton.Left && press != lastmouseleftbtn)
            {
                lastmouseleftbtn = press;
                presschange = true;
            }
            if (button == MouseButton.Middle && press != lastmousemidbtn)
            {
                lastmousemidbtn = press;
                presschange = true;
            }
            if (button == MouseButton.Right && press != lastmouserightbtn)
            {
                lastmouserightbtn = press;
                presschange = true;
            }

            
            if (lastmousex != x || lastmousey != y)
            {
                lastmousex = x;
                lastmousey = y;
                moved = true;

            }
           // Debug.WriteLine(button + "," + x + "," + y + "," + press + "  moved=" + moved + " presschange=" + presschange);
            if (!moved && !presschange)
                return;


            if (button == MouseButton.Left)
            {
                if (points.Count < 1)
                {

                    //第一次收到，当作按下状态改变
                    points.Add(new InputPoint());
                    points[0].id = 0;
                    points[0].x = x;
                    points[0].y = y;
                    points[0].press = press;
                    points[0].move = false;

                }
                else
                {

                    if (points[0].press != press)
                    {
                        //按下状态改变
                        points[0].x = x;
                        points[0].y = y;
                        points[0].press = press;
                        points[0].move = false;
                    }
                    else
                    {
                        if (points[0].x == x && points[0].y == y)
                        {
                            //啥也没变，不发事件
                            return;
                        }
                        else
                        {
                            //位置改变
                            points[0].x = x;
                            points[0].y = y;
                            points[0].move = true;
                        }
                    }
                }
                if (OnPoint != null)
                {
                    view.InvokeGfxThread((gl) =>
                    {
                        OnPoint?.Invoke(0, points[0].x, points[0].y, points[0].press, points[0].move);
                    });

                }

            }
            if (OnMousePoint != null)
            {
                view.InvokeGfxThread((gl) =>
                {
                    OnMousePoint?.Invoke(button, x, y, press);
                });

            }

        }

        public void ParseMouseWheel(IView view, float x, float y)
        {
            if (OnMouseWheel != null)
            {
                view.InvokeGfxThread((gl) =>
                {
                    OnMouseWheel?.Invoke(x, y);
                });

            }

        }
        public deleOnPoint OnPoint
        {
            get;
            set;
        }
        public deleOnMousePoint OnMousePoint
        {
            get; set;
        }
        public deleOnWheel OnMouseWheel
        {
            get;
            set;
        }
        public deleOnKey OnKey
        {
            get;
            set;
        }
        public deleOnInputText OnInputText
        {
            get;
            set;
        }
        public InputPoint[] GetInputPoints()
        {
            return points.ToArray();
        }

        public KeyCode[] GetPressKeys()
        {
            return presskeys.ToArray();
        }

        public bool IsKeyDown(KeyCode keycode)
        {
            return presskeys.Contains(keycode);
        }
        public bool SupportPrompt => false;
        public Task<string> Prompt(string message, string deftxt, int maxlen, string font)
        {
            return null;//
        }


    }
}
